#include "LockObj.h"

LockObj::LockObj()
{
	InitializeCriticalSection(&_CS);
}

LockObj::~LockObj()
{
	if(_isLocked)
		LeaveCriticalSection(&_CS);

	DeleteCriticalSection(&_CS);
}

void LockObj::Lock()
{
	EnterCriticalSection(&_CS);
	_isLocked = true;
}

void LockObj::UnLock()
{
	if(!_isLocked)
		return;

	LeaveCriticalSection(&_CS);
	_isLocked = false;
}

bool LockObj::TryLocking()
{
	bool Attempt = TryEnterCriticalSection(&_CS)?true:false;;

	if(Attempt)
		_isLocked = true;

	return Attempt;
}

bool LockObj::IsLocked()
{
	bool locked = false;

	Lock();
	locked = _isLocked;
	UnLock();

	return _isLocked;
}